package com.newbsd.web.maventest.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.newbsd.web.maventest.dao.OrdersDao;
import com.newbsd.web.maventest.pojo.Order;
import com.newbsd.web.maventest.pojo.Ticket;



/**  
*   
* @author ZHM
* 
*/
public class OrdersImpl implements OrdersDao{

	 Connection conn = null;
	
	@Override
	public Ticket getOrders(Integer ticketid) {
		
		Ticket ticket = null;
		try {
			
			Context context = new InitialContext();
			DataSource datasource = (DataSource)context.lookup("java:comp/env/jdbc/maventest");
			conn = ((PreparedStatement) datasource).getConnection();
			PreparedStatement pstmt = 
					conn.prepareStatement("select traino,origin,destination,price from ticket where ticketid=?");
			pstmt.setInt(1, ticketid);
			ResultSet rs = pstmt.executeQuery();
		while (rs.next()) {
			ticket= new Ticket();
			ticket.setTrainno(rs.getString("trainno"));
			ticket.setOrigin(rs.getString("origin"));
			ticket.setDestination(rs.getString("destination"));
			ticket.setPrice(rs.getDouble("price"));
			
			
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ticket;
		
	}
	
	@Override
	public void createOrders(Order order) {
		
		try {
			
			String sql = "insert into orders(ticketid,username,cert,trainno,origin,destination,pstatus,cstatus,price)"
			             +"values(?,?,?,?,?,?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, order.getTicketid());
			pstmt.setString(2, order.getUsername());
			pstmt.setInt(3, order.getCert());
			pstmt.setString(4, order.getTrainno());
			pstmt.setString(5, order.getOrigin());
			pstmt.setString(6, order.getDestination());
			pstmt.setInt(7, order.getPstatus());
			pstmt.setInt(8, order.getCstatus());
			pstmt.setDouble(9, order.getPrice());
			
			pstmt.execute();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
	}

	@Override
	public void detailOrders() {
		Order order = null;
		try {
			PreparedStatement pstmt = 
					conn.prepareStatement("select * from orders");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				order = new Order();
				order.setCert(rs.getInt("cert"));
				order.setCstatus(rs.getInt("cstatus"));
				order.setDestination(rs.getString("destination"));
				order.setOrderid(rs.getInt("orderid"));
				order.setOrigin(rs.getString("origin"));
				order.setPrice(rs.getDouble("price"));
				order.setPstatus(rs.getInt("pstatus"));
				order.setTicketid(rs.getInt("ticketid"));
				order.setTrainno(rs.getString("trainno"));
				order.setUsername(rs.getString("username"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (conn!=null) {
				try {
					conn.close();
					conn = null;
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			
			 
		}
	}
	
	

}
